package l1j.server.server.clientpackets;

import static l1j.server.server.model.skill.L1SkillId.SHAPE_CHANGE;
import l1j.server.echo.ClientThread;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.serverpackets.S_DoActionGFX;


import l1j.server.server.serverpackets.S_NPCTalkReturn;

// Referenced classes of package l1j.server.server.clientpackets:
// ClientBasePacket
import java.util.logging.Level;
import java.util.logging.Logger;
/**
 * 處理收到由客戶端傳來額外動作指令的封包
 */
public class C_ExtraCommand extends ClientBasePacket {
	private static final String C_EXTRA_COMMAND = "[C] C_ExtraCommand";
	private static final Logger _log = Logger.getLogger(C_ExtraCommand.class.getName());
	@Override
	public String getType() {
		return C_EXTRA_COMMAND;
	}

	@Override
	public void start(final byte[] decrypt, final ClientThread client) {
		try {
			// 資料載入
			this.read(decrypt);

			final L1PcInstance pc = client.getActiveChar();
			if (pc == null) {
				return;
			}
			/**actionId
			 * ALT + 1 = 68
			 * ALT + 2 = 69
			 * ALT + 3 = 67
			 * ALT + 4 = 66
			 * */
			final int actionId = this.readC();
			
			if (pc.isTeleport()) { // 傳送中
				return;
			}
			if(actionId == 68){
				pc.sendPackets(new S_NPCTalkReturn(pc.getId(), "newhigoBB", pc.getConfig().getValue()));
			}
			if (pc.isGhost()) {
				return;
			}
			if (pc.isInvisble()) { // 隱形中
				return;
			}
			
			if (pc.hasSkillEffect(SHAPE_CHANGE)) { // 變身中
				final int gfxId = pc.getTempCharGfx();
				if (gfxId != 6080 && gfxId != 6094) { // 騎馬用的變身例外
					return;
				}
			}
			final S_DoActionGFX gfx = new S_DoActionGFX(pc.getId(), actionId);
			pc.broadcastPacketAll(gfx); // 將動作送給附近的玩家
		} catch (final Exception e) {
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);

		} finally {
			this.over();
		}
	}
}
